import { Snake } from '../entities/Snake';

export interface Skin {
    id: string;
    name: string;
    description: string;
    unlocked: boolean;
    requiredScore: number;
}

export interface Upgrade {
    id: string;
    name: string;
    level: number;
    maxLevel: number;
    cost: number;
    effect: number;
}

export interface Achievement {
    id: string;
    name: string;
    description: string;
    progress: number;
    target: number;
    completed: boolean;
    reward: number;
}

export class ProgressionManager {
    private skins: Map<string, Skin> = new Map();
    private upgrades: Map<string, Upgrade> = new Map();
    private achievements: Map<string, Achievement> = new Map();
    private currentSkin: string = 'default';
    private score: number = 0;

    constructor() {
        this.initializeSkins();
        this.initializeUpgrades();
        this.initializeAchievements();
    }

    private initializeSkins(): void {
        this.skins.set('default', {
            id: 'default',
            name: '经典皮肤',
            description: '默认的蛇皮肤',
            unlocked: true,
            requiredScore: 0
        });

        this.skins.set('neon', {
            id: 'neon',
            name: '霓虹皮肤',
            description: '闪耀的霓虹效果',
            unlocked: false,
            requiredScore: 1000
        });

        this.skins.set('rainbow', {
            id: 'rainbow',
            name: '彩虹皮肤',
            description: '绚丽的彩虹主题',
            unlocked: false,
            requiredScore: 2000
        });
    }

    private initializeUpgrades(): void {
        this.upgrades.set('length', {
            id: 'length',
            name: '长度增益',
            level: 1,
            maxLevel: 5,
            cost: 100,
            effect: 1.2
        });

        this.upgrades.set('speed', {
            id: 'speed',
            name: '速度强化',
            level: 1,
            maxLevel: 5,
            cost: 150,
            effect: 1.1
        });

        this.upgrades.set('range', {
            id: 'range',
            name: '吞噬范围',
            level: 1,
            maxLevel: 3,
            cost: 200,
            effect: 1.3
        });
    }

    private initializeAchievements(): void {
        this.achievements.set('turnMaster', {
            id: 'turnMaster',
            name: '转向大师',
            description: '连续完成10次急转弯',
            progress: 0,
            target: 10,
            completed: false,
            reward: 500
        });

        this.achievements.set('dodgeExpert', {
            id: 'dodgeExpert',
            name: '闪避专家',
            description: '在距离障碍物1格内成功闪避10次',
            progress: 0,
            target: 10,
            completed: false,
            reward: 300
        });
    }

    public updateScore(score: number): void {
        this.score = score;
        this.checkSkinUnlocks();
    }

    private checkSkinUnlocks(): void {
        this.skins.forEach(skin => {
            if (!skin.unlocked && this.score >= skin.requiredScore) {
                skin.unlocked = true;
            }
        });
    }

    public upgradeAttribute(id: string): boolean {
        const upgrade = this.upgrades.get(id);
        if (!upgrade || upgrade.level >= upgrade.maxLevel || this.score < upgrade.cost) {
            return false;
        }

        this.score -= upgrade.cost;
        upgrade.level++;
        upgrade.cost = Math.floor(upgrade.cost * 1.5);
        return true;
    }

    public updateAchievement(id: string, progress: number): void {
        const achievement = this.achievements.get(id);
        if (!achievement || achievement.completed) return;

        achievement.progress = Math.min(achievement.progress + progress, achievement.target);
        if (achievement.progress >= achievement.target) {
            achievement.completed = true;
            this.score += achievement.reward;
        }
    }

    public getUpgradeEffect(id: string): number {
        const upgrade = this.upgrades.get(id);
        return upgrade ? Math.pow(upgrade.effect, upgrade.level - 1) : 1;
    }

    public setCurrentSkin(id: string): boolean {
        const skin = this.skins.get(id);
        if (!skin || !skin.unlocked) return false;

        this.currentSkin = id;
        return true;
    }

    public getCurrentSkin(): string {
        return this.currentSkin;
    }

    public getSkins(): Skin[] {
        return Array.from(this.skins.values());
    }

    public getUpgrades(): Upgrade[] {
        return Array.from(this.upgrades.values());
    }

    public getAchievements(): Achievement[] {
        return Array.from(this.achievements.values());
    }

    public getScore(): number {
        return this.score;
    }

    public reset(): void {
        this.score = 0;
        this.currentSkin = 'default';
        
        // 重置所有皮肤状态
        this.skins.clear();
        this.initializeSkins();
        
        // 重置所有升级状态
        this.upgrades.clear();
        this.initializeUpgrades();
        
        // 重置所有成就状态
        this.achievements.clear();
        this.initializeAchievements();
    }
}